package gov.kqqzyyy.monkey.orderpad.adapter;

import android.content.Context;
import androidx.core.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.mytools.MyStringUtil;
import gov.kqqzyyy.monkey.orderpad.object.SelectedDish;


/**
 * Created by Monkey on 2017/10/17.
 * 忌口适配器
 */

public class TasteAdapter extends BaseAdapter {

    private List<SelectedDish> list;
    private Context mContext;
    private LayoutInflater inflater;

    public TasteAdapter(Context context) {
        mContext = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<SelectedDish> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.get(position) != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_order_submit_select, parent, false);
            viewHolder.tasteText = convertView.findViewById(R.id.order_submit_selectnum);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SelectedDish dish = list.get(position);
        if (dish != null) {
            String text = MyStringUtil.doHideCPMC(dish.getCPMC()) + " * " + dish.getCPSL();
            viewHolder.tasteText.setText(text);
            if (dish.isJK()) {
                viewHolder.tasteText.setTextColor(ContextCompat.getColor(mContext, R.color.colorOrange));
            } else {
                viewHolder.tasteText.setTextColor(ContextCompat.getColor(mContext, R.color.colorBlack));
            }
        }

        return convertView;
    }

    private class ViewHolder {
        private TextView tasteText;
    }
}
